//
//  ContentTableViewCell.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/5/3.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ContentTableViewCell.h"
#import "UIView+Extension.h"
@implementation ContentTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle=0;
        self.shareBtn=[[UIButton alloc]initWithFrame:CGRectMake(MainW-40, 0, 40, 38)];
        //self.shareBtn.backgroundColor=[UIColor redColor];
        [self.contentView addSubview:self.shareBtn];
    }
        return self;
}
-(void)setModel:(DetailModel *)model{
    
    if (![_model isEqual:model]&&model!=nil) {
        _model=model;
         [self creatShopView];
    }
   
}
-(void)creatShopView{
    int count = 0;
    float btnWidth = 0;
    float viewHeight = 0;
    for (int i = 0; i < self.model.tags.count; i++) {
        
        NSString *btnName = self.model.tags[i][@"name"];
        
        _byLabel = [[UILabel alloc]init];
        [_byLabel setBackgroundColor:Red_Color];
        [_byLabel setTextColor:[UIColor whiteColor]];
        _byLabel.textAlignment=1;
        _byLabel.text=btnName;
        _byLabel.font = FontAndStyle12;
        ViewRadius(_byLabel, 1);
        //NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(12) forKey:NSFontAttributeName];
        CGSize btnSize = [_byLabel.text sizeWithAttributes:@{NSFontAttributeName:_byLabel.font }];
        
        _byLabel.width = btnSize.width+5;
        _byLabel.height = btnSize.height+3;
        
        if (i==0)
        {
            _byLabel.x = 10;
            btnWidth = CGRectGetMaxX(_byLabel.frame);
        }
        else{
            btnWidth += CGRectGetMaxX(_byLabel.frame)+10;
            if (btnWidth > MainW) {
                count++;
                _byLabel.x = 10;
                btnWidth = CGRectGetMaxX(_byLabel.frame);
            }else{
                _byLabel.x += btnWidth - _byLabel.bsg_width;
            }
        }
        _byLabel.y += count * (_byLabel.bsg_height+10)+5;
        
        viewHeight = CGRectGetMaxY(_byLabel.frame)+10;
        
        [self.contentView addSubview:_byLabel];
        
        _byLabel.tag = 10000+i;
        
    }
    self.bsg_height = viewHeight;
    if (viewHeight==0) {
        viewHeight=10;
    }
    self.height = self.bsg_height;
    self.shopName=[[UILabel alloc]initWithFrame:CGRectMake(10, viewHeight, MainW, 15)];
    self.shopName.text=self.model.title;
    self.shopName.font=FontAndStyle14;
    self.shopName.textColor=TextColor3;
    [self.contentView addSubview:self.shopName];
    self.viewHeight=0;
    for (int i = 0; i < self.model.serves.count; i++) {
        
        NSString *btnName = self.model.serves[i][@"name"];
        
        self.qtlabel = [[UILabel alloc]init];
        [self.qtlabel setTextColor:Red_Color];
        self.qtlabel.textAlignment=1;
        self.qtlabel.text=btnName;
        self.qtlabel.font = FontAndStyle12;
        //ViewRadius(label, 3);
        //NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(12) forKey:NSFontAttributeName];
        CGSize btnSize = [self.qtlabel.text sizeWithAttributes:@{NSFontAttributeName:self.qtlabel.font }];
        
        self.qtlabel.width = btnSize.width ;
        self.qtlabel.height = btnSize.height;
        
        if (i==0)
        {
            self.qtlabel.x = 10;
            btnWidth = CGRectGetMaxX(self.qtlabel.frame);
        }
        else{
            btnWidth += CGRectGetMaxX(self.qtlabel.frame)+10;
            if (btnWidth > MainW) {
                count++;
                self.qtlabel.x = 10;
                btnWidth = CGRectGetMaxX(self.qtlabel.frame);
            }else{
                self.qtlabel.x += btnWidth - self.qtlabel.bsg_width;
            }
        }
        self.qtlabel.y += count * (self.qtlabel.bsg_height+10)+self.shopName.bsg_bottom+10;
        
        self.viewHeight = CGRectGetMaxY(self.qtlabel.frame)+10-self.shopName.bsg_bottom-10;
        
        [self.contentView addSubview:self.qtlabel];
        
        self.qtlabel.tag = 10000+i;
        
    }
//    if (self.viewHeight==0) {
//        self.viewHeight=10;
//    }
    if (_model.serves.count==0&&_model.tags.count==0) {
        self.viewHeight=15;
    }
    self.tline=[[UIView alloc]initWithFrame:CGRectMake(10, self.shopName.bsg_bottom+10+self.viewHeight, MainW-10, 0.5)];
    [self.tline setBackgroundColor:SeparatorColor];
    [self.contentView addSubview:self.tline];
    self.bline=[[UIView alloc]initWithFrame:CGRectMake(10, self.tline.bsg_bottom+40, MainW-10, 0.5)];
    [self.bline setBackgroundColor:SeparatorColor];
    [self.contentView addSubview:self.bline];
    self.moneyLabel=[[UILabel alloc]init];
    self.moneyLabel.textColor=TextColor6;
    self.moneyLabel.font=FontAndStyle12;
    [self.contentView addSubview:self.moneyLabel];
    self.priceLabel=[[UILabel alloc]init];
    self.priceLabel.textColor=Red_Color;
    self.priceLabel.font=FontAndStyle14;
    [self.contentView addSubview:self.priceLabel];
    self.titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, self.bline.bsg_bottom+10, MainW, 20)];
    self.titleLabel.font=FontAndStyle14;
    self.titleLabel.textColor=TextColor3;
    [self.contentView addSubview:self.titleLabel];
    self.introLabel=[[UILabel alloc]init];
    self.introLabel.numberOfLines=0;
    self.introLabel.font=FontAndStyle12;
    self.introLabel.textColor=TextColor6;
    [self.contentView addSubview:self.introLabel];
    UIImageView*shareIcon=[[UIImageView alloc]initWithFrame:CGRectMake(MainW-33, 5, 18, 16)];
    shareIcon.image=[UIImage imageNamed:@"icon_share"];
    [self.contentView addSubview:shareIcon];
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(shareIcon.bsg_x-6, shareIcon.bsg_bottom+5, 30, 10)];
    label.text=MyString(@"分享");
    label.textAlignment=1;
    label.font=[UIFont systemFontOfSize:11];
    label.textColor=TextColor6;
    [self.contentView addSubview:label];
    CGSize size=[label.text sizeWithAttributes:@{NSFontAttributeName:label.font }];
    label.frame=CGRectMake(shareIcon.bsg_x-6, shareIcon.bsg_bottom+5, size.width, 10);
    
    
    
    
    self.moneyLabel.text=[NSString stringWithFormat:@"¥%@",self.model.price];
    CGSize lSize=[self.moneyLabel.text sizeWithAttributes:@{NSFontAttributeName:self.moneyLabel.font }];
    self.moneyLabel.frame=CGRectMake(10, self.tline.bsg_bottom+12.5, lSize.width, 15);
    NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",self.moneyLabel.text]];
    [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
    self.moneyLabel.attributedText = newPrice;
    self.priceLabel.text=[NSString stringWithFormat:@"¥ %@",self.model.fixedPrice];
    self.priceLabel.frame=CGRectMake(self.moneyLabel.bsg_right+10, self.moneyLabel.bsg_centerY-10, MainW/2, 20);
    self.titleLabel.text=[NSString stringWithFormat:@
                          "%@:",self.model.introTitle];
    self.introLabel.text=_model.intro;
    CGFloat height=[ZJUITools getHeightByWidth:MainW-20 title:_model.intro font:FontAndStyle12];
    self.introLabel.frame=CGRectMake(10, self.titleLabel.bsg_bottom+5, MainW-20, height);
    if ([self.model.price intValue]==[_model.fixedPrice intValue]) {
        self.moneyLabel.hidden=YES;
        self.priceLabel.frame=CGRectMake(10, self.tline.bsg_bottom+12.5, MainW/2, 15);
    }
    
    
    
    
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
